package com.enjoy.seata.service.impl;

import com.enjoy.seata.service.AccountService;
import com.enjoy.seata.service.OrderService;
import com.enjoy.seata.util.DataSourceUtil;


public class OrderServiceImpl implements OrderService {

    private AccountService accountService;

    public static final String DB_CODE = "order";

    @Override
    public void create(String userId, String commodityCode, int orderCount) {
        int money = orderCount * 200;
        String sql = "insert into order_tbl (user_id, commodity_code, count, money) values ('" + userId + "','" + commodityCode + "'," + orderCount + "," + money + ")";

        DataSourceUtil.executeUpdate(DB_CODE, sql);

        accountService.debit(userId, money);
    }

    @Override
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public void reset(String key, String value) {

        String deleteSql = "delete from order_tbl";

        DataSourceUtil.executeUpdate(DB_CODE, deleteSql);
    }
}
